<?php


/**
 * Skeleton subclass for representing a row from the 'seo_urls' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package    lib.model
 */
class SeoUrl extends BaseSeoUrl {
	
	/**
	 * @var SeoUrl
	 */
	protected $parentUrl = null;

	/**
	 * Code to be run before inserting to database
	 * @param PropelPDO $con
	 * @return boolean
	 */
	public function preInsert(PropelPDO $con = null)
	{
    	if(!$this->getParentUrlId() && $this->parentUrl);
			$this->setParentUrlId($this->parentUrl->getId());
			
		return parent::preInsert($con);
	}
	
	/**
	 * @return string
	 */
	public function getFullUrl()
	{
		return $this->getProtocol() . '://' . $this->getDomain() . ':' . $this->getPort() . $this->getPath();
	}
	
	/**
	 * @param SeoUrl $parentUrl
	 */
	public function setParentUrl(SeoUrl $parentUrl)
	{
		$this->parentUrl = $parentUrl;
		$this->setParentUrlId($parentUrl->getId());
	}
	
	/**
	 * @param array<SeoSitePath> $paths
	 * @return bool
	 */
	public function matchesPaths(array $paths)
	{
		foreach($paths as $path)
			if(!$path->matches($this))
				return false;
				
		return true;
	}
	
	/**
	 * @param string $path
	 * @return SeoUrl
	 */
	public function getLinkUrl($path)
	{
		if(preg_match('/^javascript:/', $path))
			return null;
			
		if(preg_match('/^(.+)#[^\/]*/', $path))
			$path = preg_replace('/^(.+)#[^\/]*$/', '$1', $path);
	
		$strRegex = "^(https?):\\/\\/" . "?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?" . //user@
					"(([0-9]{1,3}\\.){3}[0-9]{1,3}" . // IP- 199.194.52.184
					"|" . // allows either IP or domain
					"([0-9a-z_!~*'()-]+\\.)*" . // tertiary domain(s)- www.
					"([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." . // second level domain
					"[a-z]{2,6})" . // first level domain- .com or .museum
					"(:([0-9]{1,4}))?" . // port number- :80
					"((\\/?)|" . // a slash isn't required if there is no file name
					"(\\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+)" . // path
					"\\/?" . // closing slash
					"$";
		
		if(preg_match("/$strRegex/i", $path, $matches))
		{
//			Ezer_Log::debug("Parsed url [$path] matches:\n" . print_r($matches, true));
			
			$url = new SeoUrl();
			$url->setProtocol($matches[1]);
			$url->setDomain($matches[4]);
			$url->setParentUrl($this);
			
			if($matches[10] && strlen($matches[10]))
				$url->setPath($matches[10]);
			
			if($matches[9] && is_numeric($matches[9]))
				$url->setPort($matches[9]);
			
			return $url;
		}
		
		if(preg_match('/^\//', $path))
		{
			$url = new SeoUrl();
			$url->setProtocol($this->getProtocol());
			$url->setDomain($this->getDomain());
			$url->setPort($this->getPort());
			$url->setPath($path);
			$url->setParentUrl($this);
			return $url;
		}
		
		$dir = dirname($this->getPath());
		$dir = str_replace('\\', '/', $dir);
		
		$url = new SeoUrl();
		$url->setProtocol($this->getProtocol());
		$url->setDomain($this->getDomain());
		$url->setPort($this->getPort());
		$url->setPath($dir . $path);
		$url->setParentUrl($this);
		return $url;
	}
	
} // SeoUrl
